<?php

class Tool extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->service('Service_urls');
        $this->load->service('Service_icon');
        $this->load->model('Model_urls');
        $this->load->driver('cache');
        $this->load->library('UrlUtil');
        $this->load->helper('url');
        $this->load->library('HttpUtil');
    }

    /**
     * 工具页
     */
    public function index()
    {
        $list = $this->_indexList();
        $this->assign('list', $list);
        $this->display('tool/index.tpl');
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $params = $this->getParams();
        $type = $params['type'];
        $id = (int)$params['id'];
        if (!in_array($type, ['edit', 'hide']) || empty($id)) {
            echo "参数错误！";
            redirect('https://idea.dreamthere.cn/tool/index');
        }

        if ($type == 'hide') {
            $res = $this->Model_urls->update(['id' => $params['id'], 'show' => 0]);
            if ($res) {
                echo "隐藏成功";
                $file = APPPATH . "../static/site/index/$id.html";
                if (file_exists($file)) {
                    unlink($file);
                }
            } else {
                echo "隐藏失败";
            }
            sleep(3);
            redirect('https://idea.dreamthere.cn/tool/index');
            exit();
        }

        $info = $this->Service_urls->get($id, true);
        $host = UrlUtil::getHost($info['url']);
        $data = [
            'id' => $info['id'],
            'title' => $info['title'],
            'host' => $host,
            'url' => $info['url'],
            'icon' => "https://{$host}/favicon.ico",
            'base64' => $this->_getIcon($info['url'])
        ];

        $this->assign('data', $data);
        $this->display('tool/edit.tpl');
    }

    /**
     * 保存信息
     */
    public function saveInfo()
    {
        $params = $this->getParams(false);
        if ((empty($params['url']) && empty($params['cate'])) || empty($params['id'])) {
            echo "参数错误！";
            redirect('https://idea.dreamthere.cn/tool/index');
        }

        $data = ['id' => $params['id']];
        if (!empty($params['url'])) {
            $data['icon_sina'] = $params['url'];
        }
        if (!empty($params['cate'])) {
            $data['cate'] = $params['cate'];
        }

        $res = $this->Model_urls->update($data);
        if ($res) {
            echo "修改成功";
            $file = APPPATH . "../static/site/index/{$params['id']}.html";
            if (file_exists($file)) {
                unlink($file);
            }
        } else {
            echo "修改失败";
        }
        redirect('https://idea.dreamthere.cn/tool/index');
    }

    private function _getIcon($url)
    {
        $domain = UrlUtil::getHost($url);
        $icon = $this->Service_icon->index($domain);
        return !empty($icon) ? $icon : "https://$domain/favicon.ico";
    }

    private function _indexList()
    {
        $cond = ['status' => 0, 'show' => 1, 'title !=' => ''];
        return $this->Service_urls->getList(1, $cond, 24, ['id', 'DESC']);
    }

    /**
     * 保存地址
     */
    public function save()
    {
        $params = $this->getParams();
        if (empty($params['url'])) {
            return false;
        }
        $res = $this->Service_urls->add($params['url']);
        if ($res) {
            echo "添加成功";
        } else {
            echo "添加失败";
        }
        redirect('https://idea.dreamthere.cn/tool/index');
    }

    /**
     * 刷新首页缓存
     */
    public function refreshIndex()
    {
        $key = "index_1";
        $this->cache->memcached->delete($key);
        $key = "index_1_page";
        $this->cache->memcached->delete($key);
        redirect('https://idea.dreamthere.cn/tool/index');
    }
}